package cn.zn.dept.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.zn.base.annotation.DataScope;
import cn.zn.base.common.constant.Constants;
import cn.zn.dept.entity.SysDept;
import cn.zn.dept.mapper.SysDeptMapper;
import cn.zn.dept.service.SysDeptService;
import cn.zn.dept.vo.SysDeptPageVo;
import cn.zn.redis.service.RedisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zn-admin
 * @since 2021-01-28
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Autowired
    private RedisService redisService;

    @Override
    @DataScope(deptFieldName = "d", selfEffective = false)
    public List<SysDept> selectRootDept() {
        return null;
    }

    @Override
    public List<SysDept> selectDeptList(SysDeptPageVo vo) {
        List<SysDept> depts = baseMapper.selectDeptList(vo);
        depts.forEach(item -> {
            item.setLabel(item.getShortName());
            item.setLeaf(item.getChildTotal() == 0);
        });
        return depts;
    }

    @Override
    public List<SysDept> selectDeptTree() {
        String key = Constants.ORG_KEY + Constants.ROOT_ORG_ID;
        // 从redis取部门树
        List<SysDept> redisTree = redisService.getCacheList(key);
        if (CollUtil.isNotEmpty(redisTree)) {
            List<SysDept> result = new ArrayList<>();
            result.add(redisTree.get(0));
            return result;
        }

        List<SysDept> deptList = baseMapper.selectDeptList2(Constants.ROOT_ORG_ID);
        List<SysDept> deptTree = getDeptTree(deptList);

        // 删除原有的缓存
        redisService.deleteObject(key);
        redisService.setCacheList(key, deptTree);
        return deptTree;
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list 分类表
     * @return String
     */
    private List<SysDept> getDeptTree(List<SysDept> list) {
        List<SysDept> returnList = new ArrayList<>();
        for (SysDept dept : list) {
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if ("0".equals(dept.getParentId())) {
                recursionFn(list, dept);
                returnList.add(dept);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list 菜单
     * @param dept 上级菜单
     */
    private void recursionFn(List<SysDept> list, SysDept dept) {
        // 得到子节点列表
        List<SysDept> childList = getChildren(list, dept);
        dept.setChildren(childList);
        for (SysDept tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysDept> getChildren(List<SysDept> list, SysDept dept) {
        List<SysDept> children = new ArrayList<>();
        for (SysDept n : list) {
            if (dept.getId().equals(n.getParentId())) {
                children.add(n);
            }
        }
        return children;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDept> list, SysDept dept) {
        return getChildren(list, dept).size() > 0;
    }

    @Override
    public void refreshDeptCache() {
        String key = Constants.ORG_KEY + Constants.ROOT_ORG_ID;

        List<SysDept> deptList = baseMapper.selectDeptList2(Constants.ROOT_ORG_ID);
        List<SysDept> deptTree = getDeptTree(deptList);

        // 删除原有的缓存
        redisService.deleteObject(key);
        redisService.setCacheList(key, deptTree);
    }

}
